package com.qf;

import com.qf.dao.projectDao;
import com.qf.entity.Project;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @ClassName Demo
 * @Description TODO
 * @Author 86181
 * @Date 2022/8/25 20:07
 * @Version 1.0
 **/
public class Demo {
    private InputStream resourceAsStream;
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;



    @Before
    public void init()throws Exception{
    //mybatis的核心是sqlsession 使用sqlsessionfactory去获取
     resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
    //构建对象
    SqlSessionFactoryBuilder sqlSessionFactoryBuilde = new SqlSessionFactoryBuilder();
    //工厂对象
     sqlSessionFactory = sqlSessionFactoryBuilde.build(resourceAsStream);
    //sqlSession
     sqlSession = sqlSessionFactory.openSession(true);
    //代理Dao
//   projectDao  mapper = sqlSession.getMapper(projectDao.class);
}
    @After
    //在@Test执行之后执行
    public void  close() throws IOException {
        //释放资源
        sqlSession.close();
        resourceAsStream.close();
    }
    @Test
    public void testProDao(){
        projectDao mapper = sqlSession.getMapper(projectDao.class);
        List<Project> projects = mapper.findproject();
        for (Project project:projects) {
            System.out.println(project);

        }

    }
    @Test
    public void testStaDao(){
        projectDao mapper = sqlSession.getMapper(projectDao.class);
        Project projectByPid = mapper.findProjectByPid(5);
        System.out.println(projectByPid);
    }
@Test
    public void testAdd(){
    projectDao mapper = sqlSession.getMapper(projectDao.class);
    Project project = new Project();
    project.setPname("石泉燕翔洞");
    project.setStarttime(new Date());
    project.setEndtime(new Date());
    mapper.addProject(project);
    sqlSession.commit();
}

}
